﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.CompilerServices;
using Microsoft.VisualBasic;

namespace AtfutureCodeMachine
{
    /// <summary>
    /// 扩展方法
    /// </summary>
    static class ExtendFunHelper
    {
        /// <summary>
        /// 获取安全string
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string SS(this object source)
        {
            if (null == source) return "";

            return source.ToString();
        }

        /// <summary>
        /// 获取安全string,并替换字符
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string SS(this object source,string strReplace)
        {
            if (null == source) return "";
            if (string.IsNullOrEmpty(strReplace)) return source.ToString();

            string strRet = source.ToString();
            return strRet;
        }

        

        /// <summary>
        /// 分割字符串，得第几个
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string Split(this object source,int i)
        {
            if (null == source) return "";

            string[] strs = source.ToString().Split(new char[] { '\'', ',', '_', '-', ':', '"', '=', ';' });
            if (strs.Length > i)
                return strs[i];
            return "";
        }

        /// <summary>
        /// 判断字符串是否为1
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static bool IsTrue(this object source)
        {
            if (null == source) return false;

            return source.ToString() == "1";
        }

        /// <summary>
        /// 获取安全string，把单引号编程两个单引号
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string CS(this object source)
        {
            if (null == source) return "";

            return source.ToString().Replace("'","''");//替换
        }

        /// <summary>
        /// 获取安全DateTime
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static DateTime SD(this object source)
        {
            if (null == source) return new DateTime(1900,1,1);

            try
            {
                return Convert.ToDateTime(source);
            }
            catch { } return new DateTime(1900, 1, 1);
        }

        /// <summary>
        /// 获取安全Decimal
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static Decimal SM(this object source)
        {
            if (null == source) return 0m;

            try
            {
                return Convert.ToDecimal(source);
            }
            catch { } return 0m;
        }

        /// <summary>
        /// 获取安全INT
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static int SI(this object source)
        {
            if (null == source) return 0;

            try
            {
                return Convert.ToInt32(source);
            }
            catch { } return 0;
        }

        /// <summary>
        /// 获取安全SHART
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static short SH(this object source)
        {
            if (null == source) return 0;

            try
            {
                return Convert.ToInt16(source);
            }
            catch { } return 0;
        }
    }
}
